package is.ru.honn.rumblr.test;

import is.ru.honn.rumblr.data.PostDataGateway;
import is.ru.honn.rumblr.data.UserDataGateway;
import is.ru.honn.rumblr.domain.RumblrPost;
import is.ru.honn.rumblr.domain.User;
import is.ruframework.data.RuDataAccessException;
import is.ruframework.data.RuDataAccessFactory;

import java.util.Collection;
import java.util.List;

/**
 * @author Karl J. Karlsson
 * @version 0.9, 6. Oct 2011
 */
public class Dashboard
{
    public static void main(String[] args)
    {
       new Dashboard();

    }

    public Dashboard()
    {
        RuDataAccessFactory factory = null;
        try
        {
            factory = RuDataAccessFactory.getInstance("data.xml");
        }
        catch (Exception ex)
        {
            throw new RuDataAccessException(ex.getMessage());
        }
        //Sets the DataAccess for UserDataGateway and PostDataGateway
        UserDataGateway userDataGateway = (UserDataGateway)factory.getDataAccess("userDataGateway");
        PostDataGateway postDataGateway = (PostDataGateway)factory.getDataAccess("postDataGateway");
        //Gets the user info for ru_honn2, uses the function that is implemented in UserData
        User ru_honn2 = userDataGateway.getUser("ru-honn2");
        //Gets the followers for ru_honn2, uses the function that is implemented in UserData
        List<User> usersIFollow = userDataGateway.getUsersThatIFollow(ru_honn2.getUsername());
        //Prints out the info on ru_honn2
        System.out.println("Username: " + ru_honn2.getUsername() + "\n" +
                           "Name: " + ru_honn2.getName() + "\n" +
                           "Follows: ");
        Collection<RumblrPost> iFollowPosts;
        for (User iFollow: usersIFollow)  //loops through the followers of ru_honn2 and prints them out
        {
            System.out.println(iFollow.getName());
            iFollowPosts = postDataGateway.getPosts(iFollow.getName());
            for (RumblrPost post: iFollowPosts)
            {
                System.out.println("\n" + post.render());

            }
        }
    }

}
